Ext.define('SKJF.controller.S_USERS',{
	extend: 'Ext.app.Controller',
	views: ['AKSES.v_s_users','AKSES.v_s_users_form'],
	models: ['m_s_users'],
	stores: ['s_s_users'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Lists_users',
		selector: 'Lists_users'
	}, {
		ref: 'v_s_users_form',
		selector: 'v_s_users_form'
	}, {
		ref: 'Saves_users',
		selector: 'v_s_users_form #save'
	}, {
		ref: 'Creates_users',
		selector: 'v_s_users_form #create'
	}, {
		ref: 'S_USERS',
		selector: 'S_USERS'
	}],


	init: function(){
		this.control({
			'S_USERS': {
				'afterrender': this.s_usersAfterRender
			},
			'Lists_users': {
				'itemdblclick': this.updateLists_users
			},
			'v_s_users_form button[action=save]': {
				click: this.saveV_s_users_form
			},
			'v_s_users_form button[action=cancel]': {
				click: this.cancelV_s_users_form
			}
		});
	},
	
	s_usersAfterRender: function(){
		var s_usersStore = this.getLists_users().getStore();
		s_usersStore.load({
			params: {
				GROUP_ID: gid
			}
		});
	},
	
	updateLists_users: function(me, record, item, index, e){
		var getS_USERS		= this.getS_USERS();
		var getLists_users	= this.getLists_users();
		var getV_s_users_form= this.getV_s_users_form(),
			form			= getV_s_users_form.getForm();
		var getSaves_users	= this.getSaves_users();
		var getCreates_users	= this.getCreates_users();
		
		getSaves_users.setDisabled(false);
		getCreates_users.setDisabled(true);
		getV_s_users_form.down('#user_id_field').setReadOnly(true);		
		getV_s_users_form.loadRecord(record);
		
		getLists_users.setDisabled(true);
		getV_s_users_form.setDisabled(false);
		getS_USERS.setActiveTab(getV_s_users_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getLists_users().getStore();
		var selection = this.getLists_users().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "user_id" = "'+selection.data.user_id+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	saveV_s_users_form: function(){
		var getS_USERS		= this.getS_USERS();
		var getLists_users 	= this.getLists_users();
		var getV_s_users_form= this.getV_s_users_form(),
			form			= getV_s_users_form.getForm(),
			values			= getV_s_users_form.getValues();
		var store 			= this.getStore('s_s_users');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			if(values.NEW_PASS == values.CONF_PASS)
			{
				Ext.Ajax.request({
					method: 'POST',
					url: 'c_s_users/save',
					params: {data: jsonData},
					success: function(response){
						var msg = Ext.decode(response.responseText);
						store.reload({
							callback: function(){
								var newRecordIndex = store.findBy(
									function(record, id) {
										if (record.get('user_id') === values.user_id) {
											Ext.Msg.show({
												title: 'Change Password',
												msg: msg.message,
												buttons: Ext.Msg.OK,
												icon: Ext.window.MessageBox.INFO
											});
											return true;
										}
										return false;
									}
								);
								/* getLists_users.getView().select(recordIndex); */
								getLists_users.getSelectionModel().select(newRecordIndex);
							}
						});
						
						form.reset();
						getV_s_users_form.setDisabled(true);
						getLists_users.setDisabled(false);
						getS_USERS.setActiveTab(getLists_users);
					},
					failure: function(response, action) {
						console.info(response);
						console.info(action);
						//var msg = Ext.decode(response.responseText);
						Ext.Msg.show({
							 title:'Failed',
							 msg: action.result.msg,
							 buttons: Ext.Msg.OK,
							 icon: Ext.Msg.WARNING
						});
					}
				});
			}
			else
			{
				Ext.Msg.show({
					title: 'Change Password',
					msg: 'Wrong Password or Password No Match',
					buttons: Ext.Msg.OK,
					icon: Ext.window.MessageBox.INFO,
					fn: function(){
						form.reset();
						getV_s_users_form.setDisabled(true);
						getLists_users.setDisabled(false);
						getS_USERS.setActiveTab(getLists_users);
					}
				});
			
			}
			
			
		}
	},
	
	cancelV_s_users_form: function(){
		var getS_USERS		= this.getS_USERS();
		var getLists_users	= this.getLists_users();
		var getV_s_users_form= this.getV_s_users_form(),
			form			= getV_s_users_form.getForm();
			
		form.reset();
		getV_s_users_form.setDisabled(true);
		getLists_users.setDisabled(false);
		getS_USERS.setActiveTab(getLists_users);
	}
	
});